package cn.yangphere.bean;

import java.util.List;

public class PageView<T> {

	/**
	 * 显示的记录集,分页数据
	 */
	private List<T> records;

	/**
	 * 页码开始索引和结束索引
	 */
	private PageIndex pageIndex;

	/**
	 * 总页数
	 */
	private long totalPage = 1;

	/**
	 * 每页显示记录数
	 */
	private int maxResult = 12;

	/**
	 * 当前页
	 */
	private int currentPage = 1;

	/**
	 * 总记录数
	 */
	private long totalRecord;

	/**
	 * 显示页面的开始索引
	 */
	private int firstIndex;

	private String url;

	public PageView(int maxResult, int currentPage) {
		this.maxResult = maxResult;
		this.currentPage = currentPage;
		this.firstIndex = (currentPage - 1) * maxResult;
	}
	
	public PageView(int maxResult, int currentPage, String url) {
		this.maxResult = maxResult;
		this.currentPage = currentPage;
		this.firstIndex = (currentPage - 1) * maxResult;
		this.url = url;
	}

	public void setQueryResult(QueryResult<T> qr) {
		setTotalRecord(qr.getTotalRecord());
		setRecords(qr.getResultList());
	}

	public List<T> getRecords() {
		return records;
	}

	public void setRecords(List<T> records) {
		this.records = records;
	}

	public PageIndex getPageIndex() {
		return pageIndex;
	}

	public long getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(long totalPage) {
		this.totalPage = totalPage;
		this.pageIndex = PageIndex.getPageIndex(maxResult, currentPage,
				totalPage);
	}

	public int getMaxResult() {
		return maxResult;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public long getTotalRecord() {
		return totalRecord;
	}

	public void setTotalRecord(long totalRecord) {
		this.totalRecord = totalRecord;
		setTotalPage(this.totalRecord % this.maxResult == 0 ? this.totalRecord
				/ this.maxResult : this.totalRecord / this.maxResult + 1);
	}

	public int getFirstIndex() {
		return firstIndex;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	public String getUrl() {
		return url;
	}

}
